package com.example.everying.service.feedback.impl;/*
 * 项目名称: everything
 * 文件名: UserComplaintsServiceImpl
 * 作者: 张泽阳
 * 日期: 2025/5/28
 * 描述:
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.everying.mapper.feedback.UserComplaintsMapper;
import com.example.everying.model.dto.feedback.ComplaintsPageDto;
import com.example.everying.model.entity.feedback.UserComplaints;
import com.example.everying.model.vo.feedback.UserComplaintsVo;
import com.example.everying.service.feedback.UserComplaintsService;
import com.example.everying.utils.RandomUtil;
import com.example.everying.utils.Result;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collection;

@Service
public class UserComplaintsServiceImpl extends ServiceImpl<UserComplaintsMapper, UserComplaints> implements UserComplaintsService {

    @Override
    public Result<?> addComplaints(UserComplaints userComplaints) {
        userComplaints.setCreateTime(LocalDateTime.now());
        userComplaints.setId(RandomUtil.generateId());
        int insert = baseMapper.insert(userComplaints);
        UserComplaints info = baseMapper.selectById(userComplaints.getId());
        if (insert > 0) {
            return Result.ok(info);
        }
        return Result.fail("投诉失败");
    }

    @Override
    public Result<?> list(ComplaintsPageDto complaintsPageDto) {
        UserComplaints userComplaints = new UserComplaints();
        userComplaints.setContent(complaintsPageDto.getContent());
        QueryWrapper<UserComplaints> queryWrapper = new QueryWrapper<>();
        if (complaintsPageDto.getContent() != null){
            queryWrapper.like("content", userComplaints.getContent());
        }
        Page<UserComplaints> pageParam = new Page<>(complaintsPageDto.getPage(),  complaintsPageDto.getLimit());
        IPage<UserComplaints> pages = baseMapper.selectPage(pageParam, queryWrapper);
        if (pages != null){
            return Result.ok(pages);
        }
        return Result.ok("还没有投诉信息");
    }

    @Override
    public Result<?> selectPageWithUser(Page<UserComplaintsVo> pageParam, ComplaintsPageDto complaintsPageDto) {

        return Result.ok(baseMapper.selectPageWithUser(pageParam, complaintsPageDto));
    }
}
